package org.ceteca.explica.client.partners;

import java.util.ArrayList;

import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.RegistryOT;
import org.ceteca.explica.core.installer.SubentityOT;
import org.ceteca.explica.core.partners.ContactOT;
import org.ceteca.explica.core.partners.DepartmentOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.eclipse.swt.widgets.Text;



/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class DepartmentClient extends CommonClient {


	private Text entryDepartmentCode, entryDepartmentName;
	



	public DepartmentClient() {
		super(DepartmentOT.class, "","");
		setDialogName(InterfacePartners.EDIT_DEPARTMENT_DIALOG_NAME);
		setEditGladePath(InterfacePartners.EXPLICA_EDIT_DEPARTMENT_GLADE_PATH);
		this.selectedEntity=null;

	}





	@Override
	public void clearEntityData() {
		
		
		getWidgets();
		entryDepartmentCode.setText("");
		entryDepartmentName.setText("");

		
		

	}

	public void getWidgets(){
		entryDepartmentCode=(Text)this.getWidget(InterfacePartners.EDIT_DEPARTMENT_CODE_WIDGETNAME);
		entryDepartmentName=(Text)this.getWidget(InterfacePartners.EDIT_DEPARTMENT_NAME_WIDGETNAME);
	}



	@Override
	public BaseVO getEntityData() {
		DepartmentOT dep=new DepartmentOT();
		dep.setName(entryDepartmentName.getText());

		
		if (selectedEntity!=null){
			dep.setVersion(((DepartmentOT)this.selectedEntity).getVersion());
			dep.setIdDepartment(((DepartmentOT)this.selectedEntity).getIdDepartment());
		}
		return dep;
	}









	
	

	
	@Override
	public void fillEntityData() {
		if (this.selectedEntity != null && this.entEditView != null) {
			getWidgets();
			setWidgetsValues((DepartmentOT) selectedEntity);
		}
	}

	@Override
	public Long getSelectedId() {
		return this.selectedEntity == null?null:((DepartmentOT)this.selectedEntity).getIdDepartment();
	}



	@Override
	public ArrayList<RegistryOT> queryEntityWithFormat(String fieldId,
			String value) {
		
		
	
		ArrayList<RegistryOT> result = new ArrayList<RegistryOT>();
		
		// Get all the entity registries that fit the search criteria.
		this.lRegistries = this.queryEntity(fieldId, value);
		// For each registry format the data of the entity as String[] ready to be printed out in the tree.
		
		for (int i=0; this.lRegistries != null && i<this.lRegistries.size(); i++) {
			BaseVO dep = (BaseVO)lRegistries.get(i);
			String[] lValues = dep.format(this.entityData.getIdField(), this.entityData.getLFields());
			
			
			RegistryOT reg = new RegistryOT(((DepartmentOT)dep).getIdDepartment(), "id", lValues);
			
			result.add(reg);
		}
		
		return result;
	}


	@Override
	public void selectEntity(Long idEntity) {

		if (this.lRegistries != null && this.lRegistries.size() > 0) {
			int index = this.indexOf(idEntity);

			if (index >= 0) {
				this.selectedEntity = (DepartmentOT)this.lRegistries.get(index);

			}
			else
				this.selectedEntity = null;
		}

	}
		
	

	@Override
	public void updateEntityData() {
		//((DepartmentOT)this.selectedEntity).setName(((Text)this.getWidget(InterfacePartners.EDIT_DEPARTMENT_NAME_WIDGETNAME)).getText());
		
	}











	@Override
	public void connectSubentityButtons() {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void buildSubentityMultiSelection() {
		// TODO Auto-generated method stub
		
	}





	@Override
	public ArrayList querySubentities(SubentityOT entData) {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public void setEditionMode(boolean enabled) {
		getWidgets();
		entryDepartmentCode.setEnabled(false);
		entryDepartmentName.setEnabled(enabled);
	}





	@Override
	public OperationResult validateEntity() {
		// TODO Auto-generated method stub
		return null;
	}





	@Override
	public void setWidgetsValues(BaseVO entity) {
		// TODO Auto-generated method stub
		
	}











	@Override
	public void buildSelectionSets() {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void init_specific_components() {
		// TODO Auto-generated method stub
		
	}





	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub
		
	}













}
